﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using sinetd.Services;

namespace ChatGUI
{
    public class Chat : ServiceWrapper, IDisposable
    {
        public Chat(string protocolName)
            : base("Chat", protocolName)
        {
        }

        public delegate void MessageHandler(string msg);

        public event MessageHandler MessageRecv;

        public void OnMessageRecv(string msg)
        {
            if (MessageRecv != null)
                MessageRecv(msg);
        }

        private bool const_read = true;

        public bool SignedIn = false;
        private Thread t;
        private ManualResetEventSlim mre = new ManualResetEventSlim(false);
        private string ans;
        private string rans;

        public bool Login(string username)
        {
            Protocol.Send("login " + username);
            string response = Protocol.Recv();
            string[] state = response.Split(' ');

            if (state[0] == "[OK]") SignedIn = true;

            if (SignedIn)
            {
                t = new Thread(() =>
                {
                    while (const_read)
                    {
                        try
                        {
                            ans = Protocol.Recv();
                        }
                        catch (Exception ex)
                        {
                            //form closing
                        }
                        string[] p = ans.Split();
                        if (p[0] != "")
                        {
                            rans = ans;
                            if (p[0] != "[OK]" && p[0] != "[Null]" && p[0] != "[Exception]")
                            {
                                OnMessageRecv(rans);
                            }
                            else
                            {
                                mre.Set();
                            }
                        }
                    }
                });
                t.Start();
            }

            return SignedIn;
        }

        public bool SendMsg(string msg)
        {
            mre.Reset();
            bool status = false;
            try
            {
                Protocol.Send("msg " + msg);
                mre.Wait();// WaitOne();
                string response = rans;//Protocol.Recv();
                string[] state = response.Split(' ');

                if (state[0] == "[OK]") status = true;
                if (state[0] == "[Exception]") status = false;
            }
            catch { }

            return status;
        }

        public List<string> Who()
        {
            mre.Reset();
            try
            {
                Protocol.Send("who 1");
                mre.Wait();
                string p = rans; // Protocol.Recv();
                string[] pp = p.Split();
                if (pp[0] == "[OK]")
                    return pp[1].Split(';').ToList();
            }
            catch { }

            return new List<string>();
        }

        #region IDisposable Members

        public void Dispose()
        {
            const_read = false;
            Protocol.Close();
            t.Join(500);
            t.Abort();
        }

        #endregion
    }
}
